package com.ruoyi.workorder.service.impl;

import java.util.HashMap;
import java.util.List;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.threadlocal.UserThreadLocalUtils;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.workorder.domain.SubmitReplyDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.workorder.mapper.WorkOrderMapper;
import com.ruoyi.workorder.domain.WorkOrder;
import com.ruoyi.workorder.service.IWorkOrderService;

/**
 * 工单管理Service业务层处理
 *
 * @author ruoyi
 * @date 2023-06-01
 */
@Service
public class WorkOrderServiceImpl implements IWorkOrderService {
    @Autowired
    private WorkOrderMapper workOrderMapper;
    
    /**
     * 查询工单管理
     *
     * @param id 工单管理主键
     * @return 工单管理
     */
    @Override
    public WorkOrder selectWorkOrderById(Long id) {
        return workOrderMapper.selectWorkOrderById(id);
    }
    
    /**
     * 查询工单管理列表
     *
     * @param workOrder 工单管理
     * @return 工单管理
     */
    @Override
    public List<WorkOrder> selectWorkOrderList(WorkOrder workOrder) {
        //构建map来传递参数
        HashMap<String, Object> params = new HashMap<>();
        //存储条件查询参数对象
        params.put("workOrder", workOrder);
        //存储当前登录用户名
        params.put("username", UserThreadLocalUtils.getUserName());
        return workOrderMapper.selectWorkOrderList(params);
    }
    
    /**
     * 新增工单管理
     *
     * @param workOrder 工单管理
     * @return 结果
     */
    @Override
    public int insertWorkOrder(WorkOrder workOrder) {
        //判断必填参数是否为空
        if (StringUtils.isEmpty(workOrder.getName()) || StringUtils.isEmpty(workOrder.getContent())) {
            //为空直接返回
            return 0;
        }
        //设置是否派单，默认为派单
        workOrder.setIsDispatch(false);
        //设置是否回复，默认未回复
        workOrder.setIsReply(false);
        //设置创建时间
        workOrder.setCreateTime(DateUtils.getNowDate());
        //设置创建人
        workOrder.setCreateUser(UserThreadLocalUtils.getUserName());
        return workOrderMapper.insertWorkOrder(workOrder);
    }
    
    /**
     * 修改工单管理
     *
     * @param workOrder 工单管理
     * @return 结果
     */
    @Override
    public int updateWorkOrder(WorkOrder workOrder) {
        //设置修改时间
        workOrder.setUpdateTime(DateUtils.getNowDate());
        //设置修改人
        workOrder.setUpdateUser(UserThreadLocalUtils.getUserName());
        return workOrderMapper.updateWorkOrder(workOrder);
    }
    
    /**
     * 批量删除工单管理
     *
     * @param ids 需要删除的工单管理主键
     * @return 结果
     */
    @Override
    public int deleteWorkOrderByIds(Long[] ids) {
        //遍历工单编号集合
        for (Long id : ids) {
            //根据工单编号查询工单信息
            WorkOrder workOrderDB = workOrderMapper.getById(id);
            //判空
            if (workOrderDB == null) {
                return 0;
            }
            //判断是否已回复
            if (workOrderDB.getIsReply() != null && workOrderDB.getIsReply()) {
                return 0;
            }
        }
        return workOrderMapper.deleteWorkOrderByIds(ids);
    }
    
    /**
     * 删除工单管理信息
     *
     * @param id 工单管理主键
     * @return 结果
     */
    @Override
    public int deleteWorkOrderById(Long id) {
        return workOrderMapper.deleteWorkOrderById(id);
    }
    
    /**
     * 工单管理派单
     */
    @Override
    public AjaxResult receivingOrder(Long id, String dispatchName) {
        //根据工单id查询工单信息
        WorkOrder workOrderDB = workOrderMapper.getById(id);
        //判空
        if (workOrderDB == null) {
            return AjaxResult.error("工单数据不存在");
        }
        //判断是否已经派单
        if (workOrderDB.getIsDispatch()) {
            return AjaxResult.error("已回复的不能再次派单");
        }
        //设置派单人
        workOrderDB.setDispatchName(dispatchName);
        //设置派单状态
        workOrderDB.setIsDispatch(true);
        //设置更新时间
        workOrderDB.setUpdateTime(DateUtils.getNowDate());
        //设置更新人
        workOrderDB.setUpdateUser(UserThreadLocalUtils.getUserName());
        //更新数据至数据库
        int order = workOrderMapper.updateWorkOrder(workOrderDB);
        //派单是否更新成功
        if (order == 0) {
            return AjaxResult.error("派单失败,后台错误");
        }
        return AjaxResult.success();
    }
    
    /**
     * 提交回复
     */
    @Override
    public AjaxResult submitReply(SubmitReplyDTO dto) {
        //参数审核
        if (StringUtils.isEmpty(dto.getReplyContent()) || StringUtils.isEmpty(dto.getReplyImage())) {
            return AjaxResult.error("回复的内容和图片不能为空");
        }
        //根据id查询
        WorkOrder workOrderDB = workOrderMapper.getById(dto.getId());
        //判空
        if (workOrderDB == null) {
            return AjaxResult.error("工单数据不存在");
        }
        //判断状态是否为已回复
        if (workOrderDB.getIsReply()) {
            return AjaxResult.error("已回复的工单不能再次回复");
        }
        //设置回复状态为已回复
        workOrderDB.setIsReply(true);
        //设置回复内容
        workOrderDB.setReplyContent(dto.getReplyContent());
        //设置回复图片
        workOrderDB.setReplyImage(dto.getReplyImage());
        //设置修改时间
        workOrderDB.setUpdateTime(DateUtils.getNowDate());
        //设置修改人
        workOrderDB.setUpdateUser(UserThreadLocalUtils.getUserName());
        workOrderMapper.updateWorkOrder(workOrderDB);
        return AjaxResult.success();
    }
}
